package cn.school.service;

import cn.school.dao.AdminDao;
import cn.school.dao.ExamResultDao;
import cn.school.dao.StudentDao;
import cn.school.entity.ExamResult;
import cn.school.entity.Page;
import cn.school.entity.Student;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.session.SqlSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
@Transactional(readOnly = true)
//@Scope("prototype")

public class StudentService {

    public StudentService(){
       // System.out.println("StudentService init");
    }
//    @Autowired
//    private ExamResultDao resultDao;
    @Autowired
    private StudentDao mapper;

    public Page<Student> search(Map<String, Object> m, int pageNo, int pageSize) {


        List<Student> list = mapper.search(m, pageNo, pageSize);
        int total = mapper.searchTotal(m);
        int totalPage = total / pageSize + (total % pageSize == 0 ? 0 : 1);
        return new Page<Student>(list, totalPage, pageNo, pageSize);


    }

    public Student get(int stuno) {
        return mapper.get(stuno);

    }

    @Transactional(readOnly = false)
    public int add(Student stu) {
        //模拟执行了2条sql
        int r = mapper.add(stu);
//        ExamResult result = new ExamResult();
//        result.setStId(stu.getStuno());
//        result.setSbId(25);
//        result.setScore(70);
//        result.setExamDate(new Date());
//        this.resultDao.add(result);
        return r;
    }

    public List<Map> findScore(long stuno) {
        return mapper.findScore(stuno);

    }

    /**
     * 返回true表示重复了,false表示没有
     *
     * @param phone
     * @return
     */
    public boolean checkPhoneRepeat(String phone) {
        return mapper.checkPhoneRepeat(phone) > 0;

    }
}
